package com.jy.appmarket.network.resultdata;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.jy.app.market.idata.Doc;
import com.jy.app.market.idata.card.Card;
import com.jy.app.market.idata.card.CardGroupApk;
import com.jy.app.market.idata.data.Apk;
import com.jy.app.market.idata.data.PageCard;
import com.jy.appmarket.cards.DownloadableUICard;
import com.jy.appmarket.model.AppInfoModel;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2014年8月27日
 * @version 1.0
 */

public class WidgetResultData extends BaseResultData {
	// private static final String TAG = "WidgetResultData";
	private List<AppInfoModel> mList;
	private int mGroup;

	public List<AppInfoModel> getList() {
		return mList;
	}

	public int getGroup() {
		return mGroup;
	}

	public WidgetResultData(String type) {
		// mType = type;
	}

	@Override
	public boolean parseResult(String result) {
		Doc doc = Doc.fromJson(result);
		if (doc == null) {
			return false;
		}
		setDataType(doc.getDataType());
		setStatus(doc.getStatus());
		setMessage(doc.getMessage());

		if (doc.isOk()) {
			if (doc.isType(PageCard.class)) {
				PageCard data = doc.getData();
				List<Card> mCards = data.getCards();
				if (mList == null) {
					mList = new ArrayList<AppInfoModel>();
				}

				for (int i = 0; i < mCards.size(); i++) {
					if (mCards.get(i) instanceof CardGroupApk) {
						List<Apk> apklist = ((CardGroupApk) mCards.get(i)).getApks();
						mGroup = ((CardGroupApk) mCards.get(i)).getTopId();
						for (int j = 0; j < apklist.size(); j++) {
							// 从服务器返回apk结构的数据，客户端做一下转存到apkinfomodel
							AppInfoModel appinfoitem = new AppInfoModel();
							Apk apkItem = apklist.get(j);
							if (TextUtils.isEmpty(apkItem.getPackageName()) || apkItem.getDownloadUrl() == null) {
								continue;
							}
							appinfoitem.setName(apkItem.getTitle());
							appinfoitem.setCategory("empty");
							appinfoitem.setDesc(apkItem.getDescription());
							if (apkItem.getDownloadUrl() != null) {
								appinfoitem.setDownloadUrl(apkItem.getDownloadUrl().getUrl());
							}
							appinfoitem.setDownloads(apkItem.getDownloadCountStr() + "人安装 ");
							appinfoitem.setPackageName(apkItem.getPackageName());
							appinfoitem.setPic(getIconUrl(apkItem.getIcons()));
							appinfoitem.setVersionName(apkItem.getVersionName());
							appinfoitem.setVersionCode(DownloadableUICard.safeInt(apkItem.getVersionCode()));
							appinfoitem.setMd5(apkItem.getMd5());
							appinfoitem.setSize(DownloadableUICard.safeInt(apkItem.getBytes()));

							mList.add(appinfoitem);
						}
						break;
					}
				}
				return true;
			}
		}
		return false;
	}

	private String getIconUrl(JsonObject icons) {
		if (icons == null) {
			return "";
		}
		String url = "";
		do {
			url = getJsonObjectString(icons, "px68");
			if (!TextUtils.isEmpty(url)) {
				break;
			}
			url = getJsonObjectString(icons, "px48");
			if (!TextUtils.isEmpty(url)) {
				break;
			}
			url = getJsonObjectString(icons, "px78");
			if (!TextUtils.isEmpty(url)) {
				break;
			}
		} while (false);
		if (TextUtils.isEmpty(url)) {
			for (Entry<String, JsonElement> entry : icons.entrySet()) {
				if (!entry.getValue().isJsonNull()) {
					url = entry.getValue().getAsString();
					break;
				}
			}
		}
		return url;
	}

	private String getJsonObjectString(JsonObject json, String key) {
		if (json == null || TextUtils.isEmpty(key)) {
			return null;
		}

		JsonElement ele = json.get(key);
		if (ele == null || ele.isJsonNull()) {
			return null;
		}

		return json.get(key).getAsString();
	}

	public String getFileContentString(File file) {
		FileInputStream fis;
		String content = null;
		try {
			fis = new FileInputStream(file);
			byte[] bytes = new byte[(int) file.length()];
			fis.read(bytes);
			fis.close();
			content = new String(bytes, "UTF-8");
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
}
